<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">标题</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="标题"
          style="width: 100px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">内容</label>
        <el-input
          v-model="query.content"
          clearable
          placeholder="内容"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">发布者身份</label>
        <el-select
          v-model="query.userType"
          clearable
          size="small"
          placeholder="发布者身份"
          class="filter-item"
          style="width: 115px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in dict.user_type" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">审核状态</label>
        <el-select
          v-model="query.verifyStatus"
          clearable
          size="small"
          placeholder="审核状态"
          class="filter-item"
          style="width: 115px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <el-select
          v-model="query.isEnable"
          clearable
          size="small"
          placeholder="状态"
          class="filter-item"
          style="width: 80px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in dict.data_status" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="630px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.name" style="width: 470px;" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="留言内容" prop="content">
          <el-input v-model="form.content" :rows="8" type="textarea" style="width: 470px;" placeholder="请输入给母校的寄语" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.sort" style="width: 470px;" />
        </el-form-item>
        <el-form-item label="发布者">
          <el-input v-if="false" v-show="false" v-model="form.userId" />
          <el-input v-model="form.publishUserName" style="width: 150px;" readonly="readonly" />
        </el-form-item>
        <el-form-item label="发布者身份">
          <el-radio v-for="item in dict.user_type" :key="item.id" v-model="form.userType" :label="item.value">
            {{ item.label }}
          </el-radio>
        </el-form-item>
        <el-form-item v-permission="permission.verify" label="审核状态">
          <el-select v-model="form.verifyStatus" filterable placeholder="请选择" style="width: 150px;">
            <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-for="item in dict.data_status" :key="item.id" v-model="form.isEnable" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="标题" />
      <el-table-column prop="content" label="留言内容" width="320" />
      <el-table-column prop="verifyStatus" label="审核状态" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.verifyStatus=='0'">待审核</div>
          <div v-if="scope.row.verifyStatus=='1'" style="color: limegreen">审核通过</div>
          <div v-if="scope.row.verifyStatus=='2'" style="color: #FF4949">审核未通过</div>
        </template>
      </el-table-column>
      <el-table-column prop="publisher" label="发布者">
        <template slot-scope="scope">
          <div>{{ scope.row.publisher.username }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="userType" label="发布者身份">
        <template slot-scope="scope">
          {{ dict.label.user_type[scope.row.userType] }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="reviewer" label="审核者">
        <template slot-scope="scope">
          <div>{{ scope.row.reviewer == null ? '' : scope.row.reviewer.username }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="reviewTime" label="审核时间" width="135">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isEnable" label="状态" width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isEnable" active-color="#409EFF" inactive-color="#F56C6C" @change="changeEnabled(scope.row, scope.row.isEnable)" />
        </template>
      </el-table-column>
      <el-table-column v-permission="['admin','message:edit','message:del']" label="操作" width="120px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import {
  mapGetters
} from 'vuex'
import crudMessage from '@/api/anniversary/message'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  name: null,
  userType: '0',
  content: null,
  verifyStatus: '0',
  sort: 9999,
  remark: null,
  isEnable: 'false'
}
export default {
  name: 'Message',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['user_type', 'info_verify_status', 'data_status'],
  cruds() {
    return CRUD({
      title: '留言板',
      url: 'api/message',
      sort: 'id,desc',
      crudMethod: { ...crudMessage
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'message:add'],
        edit: ['admin', 'message:edit'],
        del: ['admin', 'message:del'],
        verify: ['admin', 'message:verify']
      },
      rules: {
        name: [{
          required: true,
          message: '标题不能为空',
          trigger: 'blur'
        }],
        content: [{
          required: true,
          message: '给母校的寄语不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        form.userId = this.user.id
        form.publishUserName = this.user.username
      } else {
        form.userId = `${form.publisher.id}`
        form.publishUserName = `${form.publisher.name}`
      }
      form.userType = `${form.userType}`
      form.verifyStatus = `${form.verifyStatus}`
      form.isEnable = `${form.isEnable}`
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.data_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudMessage.edit(data).then(res => {
          this.crud.notify(this.dict.label.data_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.isEnable = !data.isEnable
        })
      }).catch(() => {
        data.isEnable = !data.isEnable
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	/deep/ .el-input-number .el-input__inner {
		text-align: left;
	}
</style>
